import time
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction

class Transfer_Accounts_Operation:
    def transfer_accounts(self,driver,payee_name,payee_account,Receiving_bank,pay_money,pay_purpose,transfer_method):
        # 建行转账
        # 点击转账汇款
        el1 = driver.find_element(MobileBy.XPATH,
                                  "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/androidx.slidingpanelayout.widget.SlidingPaneLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.viewpager.widget.ViewPager/android.widget.FrameLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View[2]/android.view.View/android.view.View[1]/android.view.View/android.view.View[3]/android.view.View")
        el1.click()
        time.sleep(4)
        # 点击银行账号转账
        el2 = driver.find_element(MobileBy.XPATH,
                                  "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/androidx.slidingpanelayout.widget.SlidingPaneLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.ImageView")
        el2.click()
        time.sleep(25)
        if payee_name == '':
            # 点击收款人花名册
            el2.click()
            time.sleep(4)
            # 点击第一个收款人
            el3 = driver.find_element(MobileBy.XPATH,
                                    "(//android.widget.RelativeLayout[@content-desc=\"黄嘉明,(六二一七三颗星一七七五),中国建设银行\"])[1]/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout")
            el3.click()
        else:
            # 输入收款人姓名
            el3 = driver.find_element(MobileBy.ID,"com.chinamworld.main:id/et_cash_name")
            el3.send_keys(payee_name)
            time.sleep(4)
            # 输入收款账号
            el4 = driver.find_element(MobileBy.ID,"com.chinamworld.main:id/et_collection_account")
            el4.send_keys(payee_account)
            time.sleep(4)
            # 关闭收款账号输入框
            TouchAction(driver).tap(x=1000, y=1466).perform()
            #打开选择所属银行
            el5 = driver.find_element(MobileBy.ID,"com.chinamworld.main:id/iv_bank_arrow")
            el5.click()
            time.sleep(4)
            if Receiving_bank == "中国建设银行":
                # 点击中国建设银行
                el6 = driver.find_element(MobileBy.XPATH,
                    "/hierarchy/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.GridView/android.widget.LinearLayout[1]")
                el6.click()
            else:
                # 搜索银行
                el6 = driver.find_element(MobileBy.ID,"com.chinamworld.main:id/search_et")
                el6.send_keys(Receiving_bank)
                time.sleep(4)
                try:
                    # 点击搜索后的第一个银行
                    el7 = driver.find_element(MobileBy.XPATH,
                        "/hierarchy/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.widget.ListView/android.view.ViewGroup/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView")
                    el7.click()
                except:
                    time.sleep(4)
                    return "收款银行不存在"
        time.sleep(4)
        # 输入转账金额
        el8 = driver.find_element(MobileBy.ID, "com.chinamworld.main:id/et_tran_amount")
        el8.send_keys(pay_money)
        time.sleep(4)
        # 打开选择付款账户
        el9 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/androidx.slidingpanelayout.widget.SlidingPaneLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.ScrollView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.LinearLayout[3]/android.widget.ImageView")
        el9.click()
        time.sleep(4)
        # 选择第一个付款账户
        el10 = driver.find_element(MobileBy.XPATH,
            "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.ListView/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout")
        el10.click()
        time.sleep(4)
        # 输入转账用途
        el11 = driver.find_element(MobileBy.ID,"com.chinamworld.main:id/et_tran_mark")
        el11.send_keys(pay_purpose)
        time.sleep(4)
        if Receiving_bank == "中国建设银行":
            # 更改转账方式
            el12 = driver.find_element(MobileBy.ACCESSIBILITY_ID, "更改")
            el12.click()
            time.sleep(4)
            if transfer_method == "实时转账":
                # 选择实时转账
                el13 = driver.find_element(MobileBy.ACCESSIBILITY_ID,"实时转账")
                el13.click()
            elif transfer_method == "次日转账":
                # 选择次日转账
                el13 = driver.find_element(MobileBy.ACCESSIBILITY_ID, "次日转账")
                el13.click()
            else:
                # 选择2小时后转账
                el13 = driver.find_element(MobileBy.ACCESSIBILITY_ID, "2小时后转账")
                el13.click()
            time.sleep(4)
        # 点击下一步
        el14 = driver.find_element(MobileBy.ID, "com.chinamworld.main:id/btn_right1")
        el14.click()
        time.sleep(4)
        try:
            try:
                # 点击确认
                el15 = driver.find_element_by_id("com.chinamworld.main:id/btn_confirm")
                el15.click()
                time.sleep(4)
            except:
                time.sleep(4)
            # 获取失败文本
            el16 = driver.find_element(MobileBy.ID, "com.chinamworld.main:id/tv_dlg_content")
            result = el16.get_attribute("text")
            time.sleep(4)
            return result
        except:
            # 点击确认
            el17 = driver.find_element(MobileBy.ID, "com.chinamworld.main:id/btn_confirm")
            el17.click()
            time.sleep(4)
            # 获取成功文本
            el18 = driver.find_element(MobileBy.ID, "com.chinamworld.main: id / title_success")
            result = el18.get_attribute("text")
            time.sleep(4)
            return result
